﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

using FuncLib.DualFunctions.Compilation;

namespace FuncLib.DualFunctions
{
	[Serializable]
	[DebuggerDisplay("{Constant}")]
	public class DualConstantFunction : DualFunction
	{
		private static DualConstantFunction zero;

		static DualConstantFunction()
		{
			zero = new DualConstantFunction(0.0);
		}

		private DualConstantFunction(double constant)
		{
			Constant = constant;
		}

		public static DualConstantFunction Create(double constant)
		{
			if (constant == 0.0)
			{
				return zero;
			}

			return new DualConstantFunction(constant);
		}

		public static bool TryCast(DualFunction function, out double constant)
		{
			if (function is DualConstantFunction)
			{
				constant = ((DualConstantFunction)function).Constant;
				return true;
			}

			constant = double.NaN;
			return false;
		}

		internal override GeneratedFunction Compile(DualCodeGenerator generator)
		{
			throw new NotImplementedException(); // FIXME ???
		}

		internal override IEnumerable<DualFunction> GetDependencies()
		{
			yield break;
		}

		public double Constant
		{
			get;
			private set;
		}
	}
}
